package com.zkjd.lingdong.ui.led

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zkjd.lingdong.model.ButtonType
import com.zkjd.lingdong.model.LedColor
import com.zkjd.lingdong.repository.DeviceEvent
import com.zkjd.lingdong.repository.DeviceRepository
import com.zkjd.lingdong.ui.home.HomeViewModel.UiEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
 * LED灯颜色设置ViewModel
 */
@HiltViewModel
class LedColorViewModel @Inject constructor(
    private val deviceRepository: DeviceRepository,
) : ViewModel() {
    
    // 工作模式颜色
    private val _connectedLedColor = MutableStateFlow<LedColor>(LedColor.GREEN)
    val connectedLedColor: StateFlow<LedColor> = _connectedLedColor.asStateFlow()
    
    // 回连模式颜色
    private val _reconnectingLedColor = MutableStateFlow<LedColor>(LedColor.RED)
    val reconnectingLedColor: StateFlow<LedColor> = _reconnectingLedColor.asStateFlow()
    
    // 当前选中的模式 (true = 工作模式, false = 回连模式)
    private val _isConnectedMode = MutableStateFlow(true)
    val isConnectedMode: StateFlow<Boolean> = _isConnectedMode.asStateFlow()
    
    // 当前活跃的颜色（根据选中的模式决定）
    val currentActiveColor: StateFlow<LedColor> get() = 
        if (_isConnectedMode.value) connectedLedColor else reconnectingLedColor
    
    // 预设颜色列表
    val presetColors = listOf(
        LedColor.RED,
        LedColor.ORANGE,
        LedColor.YELLOW,
        LedColor.GREEN,
        LedColor.BLUE,

        LedColor.MAGENTA,
        LedColor.CYAN,
        LedColor.BLUE2,

    )
    
    // UI事件
    private val _uiEvents = MutableSharedFlow<UiEvent>()
    val uiEvents: SharedFlow<UiEvent> = _uiEvents
    
    // 当前设备MAC地址
    private var currentDeviceMac: String? = null
    
    /**
     * 加载设备颜色
     */
    fun loadDeviceColor(deviceMac: String) {
        currentDeviceMac = deviceMac
        viewModelScope.launch {
            try {
                val device = withContext(Dispatchers.IO) {
                    deviceRepository.getDeviceByMacAddress(deviceMac)
                }
                if (device != null) {
                    // 加载设备的两种颜色
                    val connectedColorInt = device.connectedLedColor
                    val reconnectingColorInt = device.reconnectingLedColor
                    
                    // 找到最接近的预设颜色或创建新的LedColor对象
                    val connectedColor = findClosestPresetColor(connectedColorInt)
                    val reconnectingColor = findClosestPresetColor(reconnectingColorInt)
                    
                    _connectedLedColor.value = connectedColor
                    _reconnectingLedColor.value = reconnectingColor
                }
            } catch (e: Exception) {
                _uiEvents.emit(UiEvent.ShowError("获取LED颜色失败: ${e.message}"))
            }
        }

        viewModelScope.launch {
            deviceRepository.getDeviceEvents().collect { event ->
                when (event) {

                    is DeviceEvent.ButtonPressed -> {
                        // 显示按键功能名称或按键类型
                        when(event.buttonType)
                        {
                            ButtonType.FONE_PESS -> _uiEvents.emit(UiEvent.ShowError("进入返控模式"))
                            ButtonType.FTWO_PESS -> _uiEvents.emit(UiEvent.ShowError("退出返控模式"))
                            else -> {}
                        }

                    }
                    is DeviceEvent.BatteryLevelChanged -> {
                        // 电量变化事件不需要UI反馈
                        if(event.level<=10)
                            _uiEvents.emit(UiEvent.ShowError("妥妥贴电池电量已过低，请及时更换电池！"))
                    }
                    else -> {}
                }
            }
        }
    }
    
    /**
     * 设置当前模式
     */
    fun setColorMode(isConnected: Boolean) {
        _isConnectedMode.value = isConnected
    }
    
    /**
     * 查找最接近的预设颜色
     */
    private fun findClosestPresetColor(colorInt: Int): LedColor {
        // 简单实现：如果颜色与预设颜色完全匹配，则返回该预设颜色
        presetColors.forEach { presetColor ->
            if (presetColor.color.toArgb() == colorInt) {
                return presetColor
            }
        }
        
        // 如果没有匹配的预设颜色，则创建一个新的LedColor对象
        return LedColor(Color(colorInt), "自定义")
    }
    
    /**
     * 选择预设颜色
     */
    fun selectPresetColor(color: LedColor) {
        if (_isConnectedMode.value) {
            _connectedLedColor.value = color
        } else {
            _reconnectingLedColor.value = color
        }
    }
    
    /**
     * 选择自定义颜色（字符串输入）
     */
    fun selectCustomColor(hexColor: String) {
        try {
            val color = LedColor.fromHex(hexColor)
            if (_isConnectedMode.value) {
                _connectedLedColor.value = color
            } else {
                _reconnectingLedColor.value = color
            }
        } catch (e: Exception) {
            viewModelScope.launch {
                _uiEvents.emit(UiEvent.ShowError("无效的颜色格式"))
            }
        }
    }
    
    /**
     * 选择自定义颜色（Color对象）
     */
    fun selectCustomColor(color: Color, name: String = "自定义") {
        val ledColor = LedColor(color, name)
        if (_isConnectedMode.value) {
            _connectedLedColor.value = ledColor
        } else {
            _reconnectingLedColor.value = ledColor
        }
    }
    
    /**
     * 应用颜色设置到设备
     */
    fun applyColorSetting() {
        val deviceMac = currentDeviceMac ?: return
        viewModelScope.launch {
            try {
                withContext(Dispatchers.IO) {
                    val device = deviceRepository.getDeviceByMacAddress(deviceMac)
                    if (device != null) {
                        // 更新设备的两种颜色
                        if(device.returnControl==1) {
                            device.connectedLedColor = _connectedLedColor.value.color.toArgb()
                            device.reconnectingLedColor = _reconnectingLedColor.value.color.toArgb()

                            deviceRepository.setLedColor(deviceMac, true, device.connectedLedColor)
                            deviceRepository.setLedColor(
                                deviceMac,
                                false,
                                device.reconnectingLedColor
                            )
                            deviceRepository.updateDevice(device)

                            //添加写入
                            deviceRepository.setColor(deviceMac, true, device.connectedLedColor)

                            _uiEvents.emit(UiEvent.ColorApplied)
                        }else{
                            _uiEvents.emit(UiEvent.ShowError("返控模式未开启，无法进行操作"))
                        }
                    }
                }

            } catch (e: Exception) {
                _uiEvents.emit(UiEvent.ShowError("保存颜色设置失败: ${e.message}"))
            }
        }
    }
    
    /**
     * UI事件
     */
    sealed class UiEvent {
        data class ShowError(val message: String) : UiEvent()
        object ColorApplied : UiEvent()
    }
} 